package session

import (
	"sync"
	"time"
	"vlog/api/dbops"
	"vlog/api/defs"
	"vlog/api/utils"
)

var sessionMap *sync.Map

func init() {
	sessionMap = &sync.Map{}
}
func nowInMilli() int64 {
	return time.Now().UnixNano() / 1000000
}
func deleteExpirSession(sid string) {
	sessionMap.Delete(sid)
	dbops.DeleteSession(sid)
}
func loadSessionsFromDB() {
	r, err := dbops.RetrieveAllSessions()
	if err != nil {
		return
	}
	r.Range(func(k, v interface{}) bool {
		ss := v.(defs.SimpleSession)
		sessionMap.Store(k, ss)
		return true
	})

}
func GenerateNewSessionId(un string) string {
	id, _ := utils.NewUUID()
	ct := nowInMilli()
	ttl := ct + 30*60*1000
	ss := &defs.SimpleSession{Username: un, TTL: ttl}
	sessionMap.Store(id, ss)
	dbops.InserSesssion(id, ttl, un)
	return id
}
func IsSessionExpired(sid string) (string, bool) {
	ss, ok := sessionMap.Load(sid)
	if ok {
		ct := nowInMilli()
		if ss.(defs.SimpleSession).TTL < ct {
			deleteExpirSession(sid)
			return "", true
		}

	}
	return "", true
}
